from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.models import User
from datetime import datetime
from .models import *
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
import json
@login_required(login_url='/user/login')
def index(request,*args,**kwargs):
	username = request.session.get('username', '')
	if not MyUser.objects.filter(username=username):
		MyUser.objects.create(username=username)
		username_ = MyUser.objects.get(username=username)
		User_cont_time = User_contents_time.objects.filter(username_id=username_.id).order_by('-time')
		where = '首页'
		return render(request, 'index2.html', locals())
	where = '首页'
	username_ = MyUser.objects.get(username=username)
	User_cont_time = User_contents_time.objects.filter(username_id=username_.id).order_by('-time')


	username_id = MyUser.objects.get(username=username).id
	objects = User_contents_time.objects.filter(username_id=username_id).order_by('-time')
	p = Paginator(list(objects), 4)
	try:
		pageinfo = p.page(id)
	except PageNotAnInteger:
		pageinfo = p.page(1)
	except EmptyPage:
		pageinfo = p.page(p.num_pages)

	return render(request, 'index2.html', locals())


def get_contents(request):
	if request.method == 'POST':
		username = request.session.get('username', '')
		if not username:
			username = MyUser.objects.create(username=username)
		contents = request.POST.get('contents')
		username = MyUser.objects.get(username=username)
		pub_time = datetime.now()

		if contents != "":
			User_contents_time.objects.create(contents=str(contents), time=pub_time, username_id=username.id)
			return redirect(reverse('front:index'))
	else:
		return redirect(reverse('front:index'))


def world(request):
	username = request.session.get('username', '')
	User_cont_time = User_contents_time.objects.all().order_by('-time')
	where = '世界'
	return render(request, 'world.html', locals())


def delete(request, id):

	obj = User_contents_time.objects.filter(id=int(id)).first()
	if obj:
		obj.delete()
	return redirect(reverse('front:index'))
def data(request):
	my_data=[{

        "position_": [116.397389,39.907623],
        "content": "终点"
    }, {

        "position_": [116.416958,39.907359],

        "content": "1号"
    }, {

        "position_": [116.416958,39.917366],
        "content": "2号"
    }, {
        "position_": [116.418331,39.934743],
        "content": "3号"
    }, {
        "position_": [116.367863,39.919209],
        "content": "4号"
    }, {
        "position_": [116.385372,39.937639],
        "content": "5号"
    }, {
        "position_": [116.397732,39.921052],
        "content": "6号"
    }, ];
	return render(request,'gddt4.html',{
            'my_data': my_data
        })

def pagi(request,id):
	username=request.session.get('username','')
	username_id=MyUser.objects.get(username=username).id
	objects=User_contents_time.objects.filter(username_id=username_id).order_by('-time')


	User_cont_time = User_contents_time.objects.filter(username_id=username_id).order_by('-time')
	p=Paginator(list(objects),4)
	try:
		pageinfo=p.page(int(id))
	except PageNotAnInteger:
		pageinfo=p.page(1)
	except EmptyPage:
		pageinfo=p.page(p.num_pages)
	return render(request,'index2.html',locals())